# Copyright (c) 2008 Alvin Wang http://cloudwizard.com/
# License New BSD
# See README for additional information

import os
import sys
import ConfigParser
from configobj import ConfigObj

class Config(object):
    data = None
    cloud_abbrev = None
    cloud_service = None
    config_name = 'default'
    config_directory = None
    config_path = None
    config_type = None
    config_filename = None
    
    def __init__(self, cloud_abbrev=None, directory=None, service=None, 
        type='user', config_name='default', action=None, autoload=True):
        self.config_type=type
        self.config_name = config_name
        self.cloud_abbrev = cloud_abbrev
        
        if service:    
            self.cloud_service = cloud_service
        if cloud_abbrev:
            self.cloud_abbrev = cloud_abbrev
        
        if directory:
            self.config_directory = directory
        else:
            if os.name == "nt":
                self.config_directory = '/Documents and Settings/' + \
                    os.environ['USERNAME'] + '/cloudwizard'
            else:
                self.config_directory = '/home/' + os.getlogin() + \
                '/.cloudwizard'
                
        if autoload:
            self.config_filename = self.config_directory + '/' + \
                self.cloud_abbrev + '.' + self.config_type + '.' \
                + self.config_name
            try:
                self.read_config(options={'file_error':True})
            except:
                print "You must run 'cwinit -c "+self.cloud_abbrev+ \
                    "' first to create user authentication file"
                sys.exit()
        else:
            self.data = ConfigObj()
        
    def get_path(self):
        """
        Get the directory of the config files, create if necessary
        """
        return self.config_directory

    def aws_keypath(self, keyname=None):
        """
        return filename of the key
        """
        return self.config_directory + '/' + self.cloud_abbrev + '.key.' \
            + keyname

    def read_config(self, options={}):
        """
        
        """
        self.data = ConfigObj(self.config_filename,options=options)
        return self.data

    def write_config(self, config_array=None, cloud_abbrev='aws', 
            type='user', config_name=None ):
        """not working"""
        
        if config_name:
            self.config_name = config_name
        if self.data.filename == None:
            self.config_filename = self.config_directory + '/' + \
                self.cloud_abbrev + '.' + self.config_type + '.' \
                + self.config_name
            self.data.filename = self.config_filename
        print "saving config filename=",self.data.filename
        self.data.write()
            # need to walk through array and assign it

    def prompt(self, question=None, help_string=None, required=False,
        default=None, varname=None, namespace=None,
        service=None, vartype='text', autocreate=None, validlist=None,
        **args):
        """Ask and store variables for config"""
        # if variables are improperly sent, raise error or log it.
        # Check if info is in INI
        #if self.config.has_key(varname):
        #  return True
        # loop until answer is verified
        if default:
            answer = raw_input( question+'('+default+')')
        else:
            answer = raw_input( question)
        # error checking through 
        if len(answer) == 0:
            answer = default
        self.data[varname]=answer
        return answer
